package light.mvc.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.Role;
import light.mvc.page.base.DropDownBox;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.Status;
import light.mvc.page.base.Tree;
import light.mvc.service.admin.RoleServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired 
	private RoleServiceI roleService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/role";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Role role, PageFilter ph) {
//		spring自动将参数注入到ph对象中
		Grid grid = new Grid();
		grid.setRows(roleService.dataGrid(role, ph));
		grid.setTotal(roleService.count(role, ph));
		return grid;
	}
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return roleService.tree();
	}
	
	@RequestMapping("/addPage")
	public String addPage() {
		return "/admin/roleAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Role role) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			roleService.add(role);
			errorCode = 0;
			errorMsg = "添加成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Integer id) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			roleService.delete(id);
			errorMsg = "删除成功！";
			errorCode = 0;
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public Role get(Integer id)  {
		return roleService.get(id);
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Integer id) {
		Role role = roleService.get(id);
		request.setAttribute("role", role);
		return "/admin/roleEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Role role) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			roleService.edit(role);
			errorCode = 0;
			errorMsg = "编辑成功！";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/grantPage")
	public String grantPage(HttpServletRequest request, Integer id) {
		Role role = roleService.get(id);
		request.setAttribute("role", role);
		return "/admin/roleGrant";
	}

	@RequestMapping("/grant")
	@ResponseBody
	public Json grant(Role role) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			roleService.grant(role);
			errorMsg = "授权成功！";
			errorCode = 0;
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/listName")
	@ResponseBody
	public List<DropDownBox> listName() {
		return roleService.listName();
	}

}
